package com.easondongh.blog.service.impl;


import com.easondongh.blog.enums.ResultEnum;
import com.easondongh.blog.enums.StateEnum;
import com.easondongh.blog.exception.BlogException;
import com.easondongh.blog.mapper.TypeMapper;
import com.easondongh.blog.pojo.Type;
import com.easondongh.blog.service.TypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 博客类型
 * @author EasonDongH
 * @date 2020/3/26 19:40
 */
@Service
@Slf4j
public class TypeServiceImpl implements TypeService {

    @Autowired
    private TypeMapper typeMapper;

    @Override
    public void save(Type type) {
        // 先查看是否存在
        Type byName = this.typeMapper.getByName(type.getTypeName());
        if(byName != null) {
            throw new BlogException(ResultEnum.BLOG_TYPE_ALREADY_EXIST);
        }
        this.typeMapper.insert(type);
    }

    @Override
    public List<Type> getAll() {
        return this.typeMapper.getAll();
    }

    @Override
    public List<Type> getTypeList() {
        return this.typeMapper.getTypeList();
    }

    @Override
    public void update(Type type) {
        this.typeMapper.update(type);
    }

    @Override
    public Type getById(Integer id) {
        return this.typeMapper.getById(id);
    }

    @Override
    public void deleteById(Integer id) {
        this.typeMapper.deleteById(id);
    }

    @Override
    public void enable(Integer id) {
        Type type = this.typeMapper.getById(id);
        if(type != null) {
            type.setEnable(StateEnum.ENABLED.getCode());
            this.typeMapper.update(type);
        }
    }

    @Override
    public void disable(Integer id) {
        Type type = this.typeMapper.getById(id);
        if(type != null) {
            type.setEnable(StateEnum.NOT_ENABLE.getCode());
            this.typeMapper.update(type);
        }
    }
}
